/**
 * @name 权限的js类
 */
var Role= function () {

    /**
     * 错误信息
     *
     * @type {{attributes: {success: boolean, message: string}}}
     */
    var errors={
        'attributes':{
            success:false,
            message:"请选择权限内容"
        }
    }

    /**
     * 初始化函数
     *
     */
    var initHandler= function () {
        Common.checkbox();
    };



    var validateHandler= function () {
        var form= $('form');
        form.validate({
            rules:{
                name:{
                    required:true,
                    minlength: 2,
                    maxlength: 32,
                    remote:{
                        url:"validate",
                        type:"get"
                    }
                }
            },
            messages:{
                name:{
                    required:"必填",
                    minlength:"至少2个字符",
                    maxlength:"最多32个字符",
                    remote:"权限已经存在"
                }
            },
            submitHandler:function (form) {
                var strings=[];
                var checkes= $('input[type=checkbox]:checked');
                if (checkes.length===0){
                    Common.alert(errors.attributes);
                    return;
                }
                checkes.each(function () {
                    strings.push($(this).val());
                });
                var params={};
                params.name= $('#name').val();
                params.description= $("#description").val();
                params.authorities= strings;
                params= JSON.stringify(params);
                $.ajax({
                    type:"post",
                    data:{"params":params},
                    dataType:"json",
                    url:"submit",
                    success:function (data) {
                        Common.alert(data);
                        if (data.success){
                            setTimeout(function () {
                                location.href="index.html";
                            },1500)
                        }
                    }
                })
            }
        })
    };

    /**
     * 删除事件的函数
     */
    var deleteHandler= function () {
        $('table').on('click','a.btn',function () {
            if (!$(this).children().hasClass('fa-times-circle')){
                return;
            }
            var id= $(this).data('id');
            parent.layer.confirm("您确定要删除该权限吗？",{
                btn:['确定','取消'],
                shade:0.8
            },function (index) {
                parent.layer.close(index);
                $.ajax({
                    type:"post",
                    url:"delete",
                    data:{"id":id},
                    dataType:'json',
                    success:function (data) {
                        Common.alert(data);
                        if (data.success){
                            setTimeout(function () {
                                location.reload(true);
                            },1500);
                        }
                    }
                })
            })
        })
    };

    return {
       submit:function () {
           //初始化的函数
           initHandler();

           validateHandler();
       },

        index:function () {
           //删除函数
            deleteHandler();

            //分页的函数
            Common.page();
        }
    }
}();